package com.bath.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

public class Util
{
	
	private static final String UTF8HEX_ENCOD = "0123456789ABCDEF";
	private static final int[]  UTF8HEX_DECOD = 
	{
	    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
	    -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
	};
	
	public static String toUTF8HEX(String value) throws UnsupportedEncodingException
	{
		return toUTF8HEX(value.getBytes("UTF-8")); 
	}
	
	public static String toUTF8HEX(byte[] value)
	{
		int  s_byte = 0; 
		int  uw_index, lw_index; 
		char uw_char,  lw_char; 
		StringBuffer response = new StringBuffer();
		for (int i = 0; i < value.length; i++) 
		{
			s_byte = value[i];
			if (s_byte < 0) s_byte = (-1 * s_byte) + 127; 
			
			uw_index = s_byte / 16; 
			lw_index = s_byte % 16;
			uw_char  = UTF8HEX_ENCOD.charAt(uw_index); 
			lw_char  = UTF8HEX_ENCOD.charAt(lw_index); 
			
			response.append(uw_char); 
			response.append(lw_char); 
		} 
		return response.toString(); 
	}
	static public String getFromUTF8HEX(String str)
	{
		if (str == null) str = "";
		String strResponse = ""; 
		byte[] pBuffer = new byte[ str.length()/2 ];
		int byte_s = 0; 
		int uw_index, lw_index; 
		char uw_char, lw_char; 
		int j = 0; 
		
		for( int i=0; i < str.length(); ) {
			uw_char = str.charAt( i++ ); 
			lw_char = str.charAt( i++ ); 
			uw_index = UTF8HEX_DECOD[ uw_char ]; 
			lw_index = UTF8HEX_DECOD[ lw_char ]; 
			byte_s = uw_index*16 + lw_index; 
			if( byte_s > 127 ) byte_s = -1*(byte_s-127); 
			
			pBuffer[j++] = (byte)byte_s; 
		} // for i
		
		try {
			strResponse = new String( pBuffer, "UTF-8" ); 
		} catch( UnsupportedEncodingException unen_ex) {
			return ""; 
		} // try {
	
		return strResponse;  
	}
	
	public static String getRemoteIP(HttpServletRequest request)
	{
		String ip = request.getHeader("x-forwarded-for");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	} 
	
	static public String space(String value, char spacer, int slotcnt)
	{
		String str = ""; 
		
		if( value.length() >= slotcnt ) return value; 
		slotcnt = slotcnt - value.length(); 
		str = value; 
		for( int i=0; i < slotcnt; i++ ) {
			str = spacer + str; 
		} // for i
		
		return str; 
	}
	
	static public boolean deleteFile(String strFilePath)
    {
		File objFile = new File( strFilePath ); 
		
		return objFile.delete(); 
    }
	
	static public String createGUID()
	{
		// -----------------------------------------------------------------------------------------------------------------
		Random objRandom = new Random(); 
		String strEncode = "01234567890ABCDEF";
		String strGUID = "";
		int nPos = 0; 
		char c = '\0'; 
		// -----------------------------------------------------------------------------------------------------------------
		for( int i=0; i < 36; i++ ) {
			if( i == 8 || i == 13 || i == 18 || i == 23 ) {
				c = '-'; 				
			} else {
				nPos = objRandom.nextInt(); 
				if( nPos < 0 ) nPos *= -1; 
				nPos = nPos % strEncode.length(); 
				c = strEncode.charAt( nPos ); 				
			} // if( i
			strGUID += c; 
		} // for i
		// -----------------------------------------------------------------------------------------------------------------
		return strGUID;  
	}
	
	public static String formatNumber( String pattern, Double value ) {
		DecimalFormat df = new DecimalFormat(pattern);
		return df.format(value);
	}
	
	public static String formatNumber( String pattern, Float value ) {
		DecimalFormat df = new DecimalFormat(pattern);
		return df.format(value);
	}
	
	public static String formatNumber( String pattern, Integer value ) {
		DecimalFormat df = new DecimalFormat(pattern);
		return df.format(value);
	}
	
	public static void main(String[] args) {
		Util u = new Util();
		try {
			System.out.println(u.toUTF8HEX("admin"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
}